/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FormPessoa.java
 *
 * Created on 17/03/2011, 19:39:26
 */

package Interface;

import Controle.ControlaQuestao;
import Interface.Combos.Itens;
import Modelo.Questao;
import Modelo.ConexaoBD;
import classes.Popup;
import classes.Validacao;
import java.awt.event.KeyEvent;
import java.sql.*;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Form para cadastro de formulários
 * @author Jonas Gualberto Diel [jonasdiel@gmail.com]
 */
public class FormQuestao extends javax.swing.JInternalFrame {

    public static Connection con;
    public static Statement stmt;
    private static String FUNCTION;

    private int questionId; //Chave primária da tabela
    
    /** Creates new form FormPessoa */
    public FormQuestao() {
        abreConexao();        
        initComponents();
        preparaComboBoxFormulario();
        preparaComboBoxQuestionCategory();
    }

    //Abre uma conexão com o banco de dados
    private void abreConexao() {
        try {
            con = ConexaoBD.getInstance().getConnection();
            stmt = con.createStatement();
        } catch (Exception e) {
            System.out.print(e);
        }
    }
    
    public void preparaComboBoxQuestionCategory()
    {
        // Populando as combos
        Combos cmb = new Combos("rshQuestionCategory");
        Map<String, Itens> m = cmb.getMap();
        QuestionCategoryId.removeAllItems(); // limpa Combo
        QuestionCategoryId.addItem("::Selecione::"); // Preenche a primeira opcao
        Set<String> chaves = m.keySet();
        Iterator<String> it = chaves.iterator();
        while (it.hasNext()) {
        String chave = it.next();
        QuestionCategoryId.addItem(chave);
        }
    }
    
    public void preparaComboBoxFormulario()
    {
        // Populando as combos
        Combos cmb = new Combos("rshForm");
        Map<String, Itens> m = cmb.getMap();
        formId.removeAllItems(); // limpa Combo        
        formId.addItem("::Selecione::"); // Preenche a primeira opcao
        Set<String> chaves = m.keySet();
        Iterator<String> it = chaves.iterator();
        while (it.hasNext()) {
        String chave = it.next();
        formId.addItem(chave);
        
        }
    }   

    public void limparCampos()
    {
        formId.setSelectedIndex(0);
        QuestionCategoryId.setSelectedIndex(0);
        isMulti.setSelected(false);
        isRequired.setSelected(false);
        isDescription.setSelected(false);
        description.setText("");
    }

    /**
     * @param aFUNCTION the FUNCTION to set
     */
    public static void setFunction(String aFUNCTION) {
        FUNCTION = aFUNCTION;
    }

     /**
     * @return the FUNCTION
     */
    public static String getFunction() {
        return FUNCTION;
    }

    public void popularCampos(int questionId)
    {
        ControlaQuestao controle = new ControlaQuestao();
        Questao questao = controle.obterQuestao(questionId);

        this.questionId = questao.getQuestionId();
        
        //Cria objeto para o combobox formId
        Combos cmbForm = new Combos("rshForm", "formId", questao.getFormId());
        Map<String, Itens> mForm = cmbForm.getMap();
        Set<String> chavesForm = mForm.keySet();
        Iterator<String> itForm = chavesForm.iterator();
        while (itForm.hasNext()) {
        String codigoFormulario = itForm.next();
        formId.setSelectedItem(codigoFormulario); //Popula o combobox de formulario     
        }
        
        //Cria objeto para o combobox questionCateguryId
        Combos cmbCategory = new Combos("rshQuestionCategory", "questionCategoryId", questao.getQuestionCategoryId());
        Map<String, Itens> mCategory = cmbCategory.getMap();
        Set<String> chavesCategory = mCategory.keySet();
        Iterator<String> itCategory = chavesCategory.iterator();
        while (itCategory.hasNext()) {
        String codigoCategoria = itCategory.next();
        QuestionCategoryId.setSelectedItem(codigoCategoria); //Popula o combobox de categoria     
        }
        
        isMulti.setSelected(questao.getIsMulti());
        isRequired.setSelected(questao.getIsRequired());
        isDescription.setSelected(questao.getIsDescriptive());
        description.setText(questao.getDescription());
    }

    /**
     * Método para validar os campos do formulário
     * @return
     */
    private boolean validarCampos()
    {
        validaDescription.setText("");
        validaFormId.setText("");
        validaQuestionCategoryId.setText("");

        boolean retorno = true;
        if( ! Validacao.validarObrigatorio(description.getText()) ) //Descrição
        {
            validaDescription.setText(Validacao.ERRO_CAMPO_OBRIGATORIO);
            retorno = false;
        }   
        if( formId.getSelectedIndex() == 0 )
        {
            validaFormId.setText(Validacao.ERRO_CAMPO_OBRIGATORIO);
            retorno = false;
        }
        if( QuestionCategoryId.getSelectedIndex() == 0 )
        {
            validaQuestionCategoryId.setText(Validacao.ERRO_CAMPO_OBRIGATORIO);
            retorno = false;
        }

        return retorno;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        buttonGroup2 = new javax.swing.ButtonGroup();
        btnLimpar = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        jInternalFrame1 = new javax.swing.JInternalFrame();
        jTabbedPane2 = new javax.swing.JTabbedPane();
        jPanel4 = new javax.swing.JPanel();
        jLabel11 = new javax.swing.JLabel();
        jTextField10 = new javax.swing.JTextField();
        jLabel12 = new javax.swing.JLabel();
        jTextField11 = new javax.swing.JTextField();
        jTextField12 = new javax.swing.JTextField();
        jLabel13 = new javax.swing.JLabel();
        jLabel14 = new javax.swing.JLabel();
        jTextField13 = new javax.swing.JTextField();
        jTextField14 = new javax.swing.JTextField();
        jLabel15 = new javax.swing.JLabel();
        jPanel5 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jTextField15 = new javax.swing.JTextField();
        jLabel17 = new javax.swing.JLabel();
        jTextField16 = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jTextField17 = new javax.swing.JTextField();
        jLabel19 = new javax.swing.JLabel();
        jTextField18 = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jComboBox2 = new javax.swing.JComboBox();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        labelDescricao = new javax.swing.JLabel();
        description = new javax.swing.JTextField();
        validaDescription = new javax.swing.JLabel();
        validaFormId = new javax.swing.JLabel();
        formId = new javax.swing.JComboBox();
        labelFormId = new javax.swing.JLabel();
        labelQuestionCategoryId = new javax.swing.JLabel();
        QuestionCategoryId = new javax.swing.JComboBox();
        validaQuestionCategoryId = new javax.swing.JLabel();
        isMulti = new javax.swing.JCheckBox();
        isRequired = new javax.swing.JCheckBox();
        isDescription = new javax.swing.JCheckBox();

        setBorder(javax.swing.BorderFactory.createEtchedBorder());
        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setIconifiable(true);
        setTitle("Cadastrar Questão");
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        setFrameIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/menu/key_q.png"))); // NOI18N
        setName("FormCategoria"); // NOI18N

        btnLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/draw_eraser.png"))); // NOI18N
        btnLimpar.setText("Limpar");
        btnLimpar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLimparActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Interface/imagens/save_as.png"))); // NOI18N
        btnSalvar.setText("Salvar");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        jInternalFrame1.setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        jInternalFrame1.setClosable(true);
        jInternalFrame1.setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        jInternalFrame1.setTitle("Cadastrar Pessoa");
        jInternalFrame1.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
        jInternalFrame1.setName("FormPessoa"); // NOI18N

        jLabel11.setText("Nome:");

        jTextField10.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextField10ActionPerformed(evt);
            }
        });

        jLabel12.setText("Apelido:");

        jLabel13.setText("E-mail:");

        jLabel14.setText("URL:");

        jLabel15.setText("E-mail alternativo:");

        javax.swing.GroupLayout jPanel4Layout = new javax.swing.GroupLayout(jPanel4);
        jPanel4.setLayout(jPanel4Layout);
        jPanel4Layout.setHorizontalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel11)
                            .addComponent(jLabel12)
                            .addComponent(jLabel13)
                            .addComponent(jLabel14))
                        .addGap(66, 66, 66))
                    .addGroup(jPanel4Layout.createSequentialGroup()
                        .addComponent(jLabel15)
                        .addGap(18, 18, 18)))
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(jTextField14, javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jTextField13)
                    .addComponent(jTextField11, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 180, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jTextField12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 319, Short.MAX_VALUE)
                    .addComponent(jTextField10, javax.swing.GroupLayout.Alignment.LEADING))
                .addContainerGap(97, Short.MAX_VALUE))
        );
        jPanel4Layout.setVerticalGroup(
            jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel4Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel11)
                    .addComponent(jTextField10, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel12)
                    .addComponent(jTextField11, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(17, 17, 17)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel13)
                    .addComponent(jTextField12, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField13, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel15))
                .addGap(18, 18, 18)
                .addGroup(jPanel4Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTextField14, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel14))
                .addContainerGap(56, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Dados pessoais", jPanel4);

        jLabel16.setText("Logradouro:");

        jLabel17.setText("Número:");

        jLabel18.setText("Complemento:");

        jLabel19.setText("Bairro:");

        jLabel20.setText("Cidade:");

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        javax.swing.GroupLayout jPanel5Layout = new javax.swing.GroupLayout(jPanel5);
        jPanel5.setLayout(jPanel5Layout);
        jPanel5Layout.setHorizontalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel16)
                    .addComponent(jLabel17)
                    .addComponent(jLabel18)
                    .addComponent(jLabel19)
                    .addComponent(jLabel20))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, 57, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, 298, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField17, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
                        .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, 196, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(84, 84, 84))
        );
        jPanel5Layout.setVerticalGroup(
            jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel5Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel20)
                    .addComponent(jComboBox2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel16)
                    .addComponent(jTextField15, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel17)
                    .addComponent(jTextField16, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel18)
                    .addComponent(jTextField17, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel5Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel19)
                    .addComponent(jTextField18, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(83, Short.MAX_VALUE))
        );

        jTabbedPane2.addTab("Endereço", jPanel5);

        jButton3.setText("Limpar");

        jButton4.setText("Salvar");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
        jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
        jInternalFrame1Layout.setHorizontalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jInternalFrame1Layout.createSequentialGroup()
                .addGap(29, 29, 29)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jInternalFrame1Layout.createSequentialGroup()
                        .addComponent(jButton4)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3))
                    .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 536, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jInternalFrame1Layout.setVerticalGroup(
            jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jInternalFrame1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jTabbedPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 266, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton4)
                    .addComponent(jButton3))
                .addContainerGap())
        );

        labelDescricao.setText("Descrição:");

        description.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                descriptionKeyPressed(evt);
            }
        });

        validaDescription.setForeground(new java.awt.Color(255, 0, 0));

        validaFormId.setForeground(new java.awt.Color(255, 0, 0));

        formId.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        formId.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                formIdActionPerformed(evt);
            }
        });
        formId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                formIdKeyPressed(evt);
            }
        });

        labelFormId.setText("Formulário:");

        labelQuestionCategoryId.setText("Categoria:");

        QuestionCategoryId.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));
        QuestionCategoryId.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                QuestionCategoryIdKeyPressed(evt);
            }
        });

        validaQuestionCategoryId.setForeground(new java.awt.Color(255, 0, 0));

        isMulti.setText("É de múltipla escolha");

        isRequired.setText("É obrigatória");

        isDescription.setText("É descritiva");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnSalvar)
                        .addGap(18, 18, 18)
                        .addComponent(btnLimpar))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(labelFormId)
                            .addComponent(labelQuestionCategoryId)
                            .addComponent(labelDescricao))
                        .addGap(59, 59, 59)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(description, javax.swing.GroupLayout.PREFERRED_SIZE, 293, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(validaDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 131, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(isRequired)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(formId, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(validaFormId, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(QuestionCategoryId, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(validaQuestionCategoryId, javax.swing.GroupLayout.PREFERRED_SIZE, 194, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(isMulti)
                            .addComponent(isDescription))))
                .addGap(24, 24, 24))
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 305, Short.MAX_VALUE)
                    .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 304, Short.MAX_VALUE)))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(labelFormId)
                        .addComponent(formId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(validaFormId, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(QuestionCategoryId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(labelQuestionCategoryId)
                    .addComponent(validaQuestionCategoryId, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(16, 16, 16)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(isMulti)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(isRequired)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(isDescription)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(labelDescricao)
                            .addComponent(description, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(validaDescription, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnSalvar)
                    .addComponent(btnLimpar))
                .addContainerGap())
            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(0, 133, Short.MAX_VALUE)
                    .addComponent(jInternalFrame1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 133, Short.MAX_VALUE)))
        );

        getAccessibleContext().setAccessibleName("");

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        //Validação dos campos
        if( this.validarCampos() )
        {           
            //Obtem valor da combo de formulario
            Combos cmbForm = new Combos("rshForm");
            Map<String, Itens> mForm = cmbForm.getMap();
            Itens dataForm = mForm.get(formId.getSelectedItem());
                        
            //Obtem valor da combo de categoria
            Combos cmbQuestion = new Combos("rshQuestionCategory");
            Map<String, Itens> mQuestionCategory = cmbQuestion.getMap();
            Itens dataQuestion = mQuestionCategory.get(QuestionCategoryId.getSelectedItem());
                   
            Questao questao = new Questao();   
            questao.setQuestionId(this.questionId);
            questao.setFormId(dataForm.getCodigo());
            questao.setQuestionCategoryId(dataQuestion.getCodigo());            
            questao.setDescription(description.getText());
            questao.setIsMulti(isMulti.isSelected());
            questao.setIsRequired(isRequired.isSelected());
            questao.setIsDescriptive(isDescription.isSelected());

            Popup info = Popup.obterInstancia();
            ControlaQuestao ctlQuestao = new ControlaQuestao(questao, null);

            if ( getFunction() == Form.FUNCTION_INSERT ) //INSERÇÃO
            {
                if (ctlQuestao.inserirQuestao())
                {
                    FormPrincipal.formQuestaoBusca.autalizarTabela(); //Atualizar a tabela do  formmulário de busca
                    if ( info.pedirConfirmacao("Registro inserido com sucesso! Adicionar novo registro?") )
                    {
                        //Prepara para inserir novo registro
                        FormPrincipal.formQuestao.setFunction(Form.FUNCTION_INSERT); //Define a função de inserção
                        FormPrincipal.formQuestao.limparCampos(); //Limpa o formulário para inserir novo registro
                        FormPrincipal.formQuestao.setTitle("Inserir Questão"); //Define o título
                        FormPrincipal.formQuestao.setVisible(true); //Abre a janela
                    }
                    else
                    {
                        //Fecha a tela
                        this.setVisible(false);
                    }
                }
                else
                {
                    //ERRO
                    info.exibirMensagem("Erro ao inserir registro", 'e');
                }
            }
            else
            {
                if ( getFunction() == Form.FUNCTION_UPDATE ) //EDIÇÃO
                {
                    if (ctlQuestao.editarQuestao())
                    {
                        FormPrincipal.formQuestaoBusca.autalizarTabela(); //Atualizar a tabela do  formmulário de busca
                        info.exibirMensagem("Edição realizada com sucesso!", 'i');
                        this.setVisible(false);
                    }
                }
            }

        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void jTextField10ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextField10ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jTextField10ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton4ActionPerformed

    private void btnLimparActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLimparActionPerformed
        limparCampos();
    }//GEN-LAST:event_btnLimparActionPerformed

    private void descriptionKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_descriptionKeyPressed
       String tecla = KeyEvent.getKeyText(evt.getKeyCode());
       if ( tecla.equalsIgnoreCase("Enter") )
       {
           this.btnSalvarActionPerformed(null);
       }
    }//GEN-LAST:event_descriptionKeyPressed

    private void formIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_formIdKeyPressed
        
}//GEN-LAST:event_formIdKeyPressed

    private void QuestionCategoryIdKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_QuestionCategoryIdKeyPressed
        // TODO add your handling code here:
    }//GEN-LAST:event_QuestionCategoryIdKeyPressed

    /**
     * Ação de selecionar a combo de formulário
     * @param evt 
     */
    private void formIdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_formIdActionPerformed
        
    }//GEN-LAST:event_formIdActionPerformed


    @Override
    public void setDefaultCloseOperation(int operation) {

        
        super.setDefaultCloseOperation(operation);
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox QuestionCategoryId;
    private javax.swing.JButton btnLimpar;
    private javax.swing.JButton btnSalvar;
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.ButtonGroup buttonGroup2;
    private javax.swing.JTextField description;
    private javax.swing.JComboBox formId;
    private javax.swing.JCheckBox isDescription;
    private javax.swing.JCheckBox isMulti;
    private javax.swing.JCheckBox isRequired;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JInternalFrame jInternalFrame1;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTabbedPane jTabbedPane2;
    private javax.swing.JTextField jTextField10;
    private javax.swing.JTextField jTextField11;
    private javax.swing.JTextField jTextField12;
    private javax.swing.JTextField jTextField13;
    private javax.swing.JTextField jTextField14;
    private javax.swing.JTextField jTextField15;
    private javax.swing.JTextField jTextField16;
    private javax.swing.JTextField jTextField17;
    private javax.swing.JTextField jTextField18;
    private javax.swing.JLabel labelDescricao;
    private javax.swing.JLabel labelFormId;
    private javax.swing.JLabel labelQuestionCategoryId;
    private javax.swing.JLabel validaDescription;
    private javax.swing.JLabel validaFormId;
    private javax.swing.JLabel validaQuestionCategoryId;
    // End of variables declaration//GEN-END:variables

}
